import { IconType, ShowModalOption } from './type'
/**
 * showToast接口封装
 * @param { String } tips 错误提示文案
 * @param { String } icon 图标类型
 * @param { Number } timer 提示显示时间
 */

export const Message = (tips: string, icon: IconType = 'none', timer = 1000) => {
  uni.showToast({
    title: tips,
    icon,
    mask: true,
    duration: timer,
  })
}

/**
 * showModal接口封装
 * @param { Object } optionsData showModal常用参数
 * @param { Function } confirmApi 点击确定的回调
 * @param { Function } cancleApi 点击取消的回调
 * @return { Promise }
 */
export const ShowModal = (
  optionsData?: ShowModalOption,
  confirmApi?: () => void,
  cancleApi?: () => void,
): Promise<any> => {
  const options = {
    title: '提示',
    content: '',
    cancelText: '取消',
    confirmText: '确定',
    showCancel: true,
  }
  return new Promise((resolve, reject) => {
    uni.showModal({
      ...options,
      ...optionsData,
      success(res) {
        if (res.confirm) {
          confirmApi?.()
          resolve(res)
        } else {
          cancleApi?.()
          reject(res)
        }
      },
      fail(res) {
        reject(res)
      },
    })
  })
}
